<?php
/**
 * 企业
 * Created by PhpStorm.
 * User: xsh
 * Date: 2018/4/16  17:00
 */

namespace app\mobile\controller;


class Company extends Base
{
    /*
    *   构造函数
    */
    function __construct(){
        parent::__construct();
    }

    public function zquery()
    {
        if(request()->isAjax()){
            $serarch = input("name");
            $where['name']=['like','%'.$serarch.'%'];
            $data = [];
            $xls_company = db('xls_company')->where($where)->limit($this->per_page)->order('id desc')->select();

            $xls_company = $this->zquery_handle($xls_company);
            $xls_company_count = db('xls_company')->where($where)->count('id');
            if($xls_company_count > $this->per_page){
                $click_for_more=true;
            }else{
                $click_for_more=false;
            }
            $data['xls_company']=$xls_company;
            $data['click_for_more']=$click_for_more;

            $this->success($data);
        }
        $xls_company = db('xls_company')->limit($this->per_page)->order('id desc')->select();
        $xls_company = $this->zquery_handle($xls_company);
        $xls_company_count = db('xls_company')->count('id');

        if($xls_company_count > $this->per_page){
            $click_for_more=true;
        }else{
            $click_for_more=false;
        }

        $this->assign('xls_company',$xls_company);
        $this->assign('click_for_more',$click_for_more);
        return $this->fetch();
    }

    public function ajax_page(){
        $page = input("page");
        $serarch = input("name");

        $start_page = ($page - 1)*$this->per_page;

        $where=[];
        if(!empty($serarch)){
            $where['name']=['like','%'.$serarch.'%'];
        }

        $xls_company = db('xls_company')->where($where)->limit("$start_page,$this->per_page")->order('id desc')->select();
        $xls_company = $this->zquery_handle($xls_company);
        $this->success($xls_company);
    }

    public function zquery_handle($array){
        if(!empty($array)){
            foreach ($array as $key=>&$value) {
                $are_id = [$value['province'],$value['city'],$value['area']];
                $where['id'] = array('in',$are_id);
                $area_result = db('area')->where($where)->field('name')->order('id asc')->select();
                $value['province'] = isset($area_result[0]['name'])?$area_result[0]['name']:'';
                $value['city'] = isset($area_result[1]['name'])?$area_result[1]['name']:'';
                $value['area'] = isset($area_result[2]['name'])?$area_result[2]['name']:'';
                $value['path_tem'] = $value['province'].$value['city'].$value['area'];

                $value['url'] = url('Company/company_condition_info',['id'=>$value['id']]);

                if(!empty($this->info_user['id'])){
                    $attention = db('attention')->where(array('uid'=>$this->info_user['id'],'company_id'=>$value['id']))->find();
                    if(!empty($attention)){
                        $value['is_attention'] = $attention['status'];
                    }else{
                        $value['is_attention'] = 0;
                    } 
                }else{
                    $value['is_attention'] = 0;
                }
            }
        }

        return $array;
    }

    public function zquery_result(){
        $id = input('id');
        $uid = $this->info_user['id'];

        $result = member_right_num($uid,'project','',false);
        if(!$result['bool']){
            $this->error('你的查询次数已用完或者没权利查询');
        }

        $project = db('xls_project')->alias('p')->join('project_type pt','pt.id=p.type')->where(array('p.company_id'=>$id))->field('p.*,pt.type_name')->select();

        $company_result = db('xls_company')->where(array('id'=>$id))->field('name')->find();

        $project_result = [];
        foreach($project as $k=>$v){
            $project_result[$v['type']]['type_name'] = $v['type_name'];
            $project_result[$v['type']]['type_array'][]= $v;
        }

        $attention_result = db('attention')->where(array('uid'=>$uid,'company_id'=>$id))->field('status')->find();

        $attention = db('attention')->where(array('uid'=>$uid,'company_id'=>$id))->find();
        if(!empty($attention)){
            $is_attention = $attention['status'];
        }else{
            $is_attention = 0;
        }

        //更新查询次数
        member_right_num($uid,'project');
        $this->assign('company_name',$company_result['name']);
        $this->assign('project',$project);
        $this->assign('is_attention',$is_attention);
        $this->assign('project_result',$project_result);
        $this->assign('attention_result',$attention_result);
        $this->assign('id',$id);
        return $this->fetch();
    }

    //查询是否有权利
    public function company_condition_info(){
        if(!$this->info_user['id'])$this->error('请先登录');

        $uid = $this->info_user['id'];

        $company_id = input('id');

        $result = member_right_num($uid,'project','',false);

        if(!$result['bool']){
            $this->error('你的查询次数已用完或者没权利查询');
        }

        $this->success('查询成功', url('Company/zquery_result',array('id'=>$company_id)));
    }


    //关注
    public function follow(){
        $company_id = input('company_id');
        $status = input('status');
        $uid = $this->info_user['id'];

        if(request()->isAjax()) {
            if(empty($uid)){
                $this->error('请先登录!');
            }

            if($status == '1' || $status == '0'){
                $result = member_right_num($uid, 'enterprise_follow_num');
     
                if(isset($result['bool'])){
                    if(!$result['bool']){
                        $this->error('你没有关注权利或者你关注次数已用完');
                    }
                }
            }

            $result_attention = db('attention')->where(array('company_id'=>$company_id,'uid'=>$uid))->find();

            if(empty($result_attention)){
                $data['uid'] = $uid;
                $data['company_id'] = $company_id;
                $data['status'] = 1;
                db('attention')->insert($data);
            }else{
                db('attention')->where(array('uid'=>$uid,'company_id'=>$company_id))->update(array('status'=>$status));
            }
        }

    }


}